package cn.itsource.pethome.system.pro.controller;

import cn.itsource.pethome.system.pet.domain.Pet;
import cn.itsource.pethome.system.pet.query.PetQuery;
import cn.itsource.pethome.system.pro.service.IProductService;
import cn.itsource.pethome.system.pro.domain.Product;
import cn.itsource.pethome.system.pro.query.ProductQuery;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;


    /**
     * 保存和修改公用的
     *
     * @param product 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product, HttpServletRequest request) {
        try {
            if (product.getId() != null)
                productService.update(product);
            else
                productService.add(product, request);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productService.delete(id);
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setSuccess(false).setMsg("操作失败");
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        try {
            Product findnoe = productService.findnoe(id);
            return AjaxResult.success().setData(findnoe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Product> list() {

        return productService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public AjaxResult findQuey(@RequestBody ProductQuery query) {
        try {
            List<Product> queryData = productService.queryData(query);
            Long count = productService.queryCount(query);
            return AjaxResult.success().setData(queryData).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @PostMapping("/pagelist")
    public AjaxResult json(@RequestBody ProductQuery query) {
        try {
            query.setState(1l);
            List<Product> byQuery = productService.queryData(query);
            Long count = productService.queryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 批量上架
     *
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        //上下架共用一个方法，通过flag来区分
        try {
            productService.onOrOffSale(1, ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success().setSuccess(false).setMsg("上架失败！" + e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        //上下架共用一个方法，通过flag来区分
        try {
            productService.onOrOffSale(0, ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success().setSuccess(false).setMsg("下架失败！" + e.getMessage());
        }
    }
}